package com.gdufe.product.controller;

import com.gdufe.product.dto.PageProductDto;
import com.gdufe.product.dto.ProductDto;
import com.gdufe.product.dto.ProductStatusDto;
import com.gdufe.common.utils.R;
import com.gdufe.common.vo.PageBeanVo;
import com.gdufe.common.entity.Product;
import com.gdufe.product.service.ProductAdminService;
import com.gdufe.product.vo.ProductsVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/product/admin")
@Slf4j
@RequiredArgsConstructor
public class ProductAdminController {

    private final ProductAdminService productAdminService;

    /**
     * 条件分页查询
     *
     * @param pageProductDto
     * @return
     */
    @GetMapping("/list")
    public R page(PageProductDto pageProductDto) {
        PageBeanVo<Product> pageBeanVo = productAdminService.pageQuery(pageProductDto);
        return R.ok().setData(pageBeanVo);
    }

    @GetMapping("/getStock/{id}")
    public R getStock(@PathVariable Integer id){
        Integer stock = productAdminService.getById(id).getStock();
        return R.ok().put("stock",stock);
    }
    @GetMapping("/products")
    public R getPros() {
        List<ProductsVo> pros = productAdminService.getPros();
        return R.ok().put("products",pros);

    }

    /**
     * 根据id查询数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R selectById(@PathVariable Long id) {
        Product product = productAdminService.getById(id);
        return R.ok().setData(product);
    }

    /**
     * 根据id修改商品售卖状态
     *
     * @param productStatusDto
     * @return
     */
    @PutMapping("/status")
    public R updateStatus(@RequestBody ProductStatusDto productStatusDto) {
        productAdminService.updateStatus(productStatusDto);
        return R.ok();
    }


    /**
     * 新增商品
     *
     * @param productDto
     * @return
     */
    @PostMapping
    public R save(@RequestBody ProductDto productDto) {
        productAdminService.insert(productDto);
        return R.ok();
    }


    /**
     * 根据id删除商品
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        productAdminService.delete(id);
        return R.ok();
    }

    /**
     * 批量删除商品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteBatch(@RequestBody List<Long> ids) {
        productAdminService.deleteBatch(ids);
        return R.ok();
    }

    /**
     * 修改商品信息
     *
     * @param productDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody ProductDto productDto) {
        productAdminService.alter(productDto);
        return R.ok();
    }

    //根据商品id获取该商品信息
    @PostMapping("/getProduct")
    public R adminSelectById(@RequestBody Long id) {
        Product info = productAdminService.getById(id);
        return R.ok().setData(info);
    }
}
